

package  clientserver; 
import java.io.*;

import java.sql.SQLException;


import java.util.List;
import java.util.Vector;

import javax.swing.text.html.HTMLDocument.Iterator;

import college.Committee;
import college.Date;
import college.Request;
import college.User;
import college.general;

import com.mysql.jdbc.ResultSet;

import clientGUI.userPanels.Controllers.ISEGateController;
import clientserver.Packet.PacketType;

import clientserver.ocsf.*;

 

public class Client extends AbstractClient 
{
  //Class variables for  Server-Client connection************************
  
 
 private 	Object ServerAnswer;
private boolean serveranswered=false;
private PacketType  ptype;

// Class variables for Client-GUI interface**************************




  //Instance variables **********************************************
  
  /**
   * The instance of the client that created this ConsoleChat.
   */


  
  //Constructors ****************************************************

  /**
   * Constructs an instance of the ClientConsole UI.
   *
   * @param host The host to connect to.
   * @param port The port to connect on.
   */
  public Client(String host, int port) 
  {

	  super(host,port);
     System.out.println("Client started");
     try {
		openConnection();
	} catch (IOException e) {
	
		e.printStackTrace();
	}
  
  }

  
  //Instance methods ************************************************
  
  /**
   * This method waits for input from the console.  Once it is 
   * received, it sends it to the client's message handler.
 * @return 
 * @throws IOException 
 * @throws InterruptedException 
   */
  

  
  public  synchronized   void SendPacketNowait(Packet msg) 
  
  {
	  ptype=msg.type;  // save packet type for  handleMessageFromServer function
	//  System.out.println("Client:Send SQL Query to server msg type:"+msg.type);
	
	  try {
		sendToServer((Object)msg);
	} 
	  
	  
	  catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
 
	 
	

	  
  }


 
//Work with UI(GUI) Methods*********************************************
  
  /**
   *  Send packet to server and wait for answer 
   * @param Packet  msg - packet to send 
   * @return could be or Query Result if Packet.type=SELECTorUPDATE
   * or  String with curent date  if Packet.type=GETDATE
   * 
   * In all cases we need to use casting before use  returned result
   */
  public  synchronized   Object SendPacketAndWait(Packet msg) 
  
  {
	  serveranswered=false;  // reset server answer receive flag
	  ptype=msg.type;  // save packet type for  handleMessageFromServer function

	  try {
		sendToServer((Object)msg);
	} catch (IOException e) {

		e.printStackTrace();
	}
	 
	  while(serveranswered==false)
	  {

		  try {
		
			wait(100);
		} catch (InterruptedException e) {

			e.printStackTrace();
		}
	  }
	
	 return ServerAnswer;
	  
  }

  /* recieves packet with Arttached File into   and send this packet to server
   *   and also adds in server MySQL table attached_files new row with  file name
   *   and rid - request id to which this file attached
   */
  public void SendFile( AttachedFile f) throws IOException, InterruptedException  
  {
int  rid=f.getRid();
	  String fname=f.getFilename();
	  Packet p = new Packet(f);
	  sendToServer((Object)p);
//	  wait(1000);
	 
	  String id=""+rid;
	  String q="INSERT INTO attachedfiles (rid,filename) VALUES("+general.addapostrophs(id)+","+general.addapostrophs(fname)+")";
System.out.println(q);
		Packet  ins;
		ins=new Packet(q);
		ins.setUpdatePacket();
		SendPacketNowait(ins);	
  
  
  }
  
  public AttachedFile GetFile( AttachedFile f) throws IOException  
  {
	  Packet p = new Packet(f);
	  p.setGetFilePacket();
	  SendPacketAndWait(p);
	  AttachedFile ans =(AttachedFile) ServerAnswer;
	  return ans;
	  
  }
  
  
   public      synchronized        String  getDate()
   {
	   Packet p= new Packet();
	   serveranswered=false;  // reset server anser receive flag
		  ptype=Packet.PacketType.GETDATE;  // save packet type for  handleMessageFromServer function 
		  try {
				sendToServer((Object)p);
			} catch (IOException e) {

				e.printStackTrace();
			}
 
			  while(serveranswered==false)
			  {

				  try {
				
					wait(100);
				} catch (InterruptedException e) {

					e.printStackTrace();
				}
			  }   
	  return  (String) ServerAnswer; 
   }
  
  //Class methods ***************************************************
  
  /**
   * This method is responsible for the creation of the Client UI.
   *
   * @param args[0] The host to connect to.
   */
  
  
 
  

  
protected void handleMessageFromServer(Object msg)
{
		
	 serveranswered=true;
	
	
	switch(ptype)
	{
	case SELECT:
	
		 ServerAnswer=(QueryResult) msg; 
		 break;
		 
	case GETDATE:
	ServerAnswer=(String)msg; break;
	
	case GETFILE:  ServerAnswer=(AttachedFile)msg; break;
	
	}	
		

	} // msgfromserver




public static void main(String[] args) 
{ 
	
  String  host = "localhost";
  int port = 1234;  //The port number


  Client cl= new Client(host,port);
  
  
  
  
  
  
/*   how to work with  date   *///  
    String t= cl.getDate();
    
  
Date  s =new Date(t);  // we must import Date from college packet!!
Date  r =new Date(t);  // we must import Date from college packet!! 
s.addDays(17);
int j=r.subdate(s);

  System.out.println("J="+j);
  
  /*


User u= new User();
u.setClient(cl);
u.setLogin("yair");
u.loadfromdb();

/*
List<User> evals=User.GetUsersbyType(cl, "ISE");

java.util.Iterator<User>  it=evals.iterator();

User u=it.next();

System.out.println("login:"+u.getLogin());
*/

}//End of Client class

}






